package com.hsj.common.system.controller;

import com.cjxch.supermybatis.base.bean.Pager;
import com.hsj.common.system.convert.SysUserRoleConvert;
import com.hsj.common.system.entity.SysUserRoleDo;
import com.hsj.common.system.service.ISysUserRoleService;
import com.hsj.common.system.vo.request.ReqSysUserRoleCreateVo;
import com.hsj.common.system.vo.request.ReqSysUserRoleUpdateVo;
import com.hsj.common.system.vo.request.ReqSysUserRolePageQueryVo;
import com.hsj.common.system.vo.response.RespSysUserRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * 用户角色关系 Controller
 * @Author: Czy
 * @Date: 2024-01-23 23:03:26
 * @Email: 314170122@qq.com
 */
@Api(tags = "用户角色关系管理")
@RestController
@RequestMapping("/sysUserRole")
@Slf4j
public class SysUserRoleController {

    private final ISysUserRoleService sysUserRoleService;
    private final SysUserRoleConvert sysUserRoleConvert;

    public SysUserRoleController(ISysUserRoleService sysUserRoleService, SysUserRoleConvert sysUserRoleConvert) {
        this.sysUserRoleService = sysUserRoleService;
        this.sysUserRoleConvert = sysUserRoleConvert;
    }

    @PostMapping("createSysUserRole")
    @ApiOperation(value = "创建用户角色关系", notes = "创建用户角色关系", httpMethod = "POST")
    public Serializable createSysUserRole(@Valid @RequestBody ReqSysUserRoleCreateVo vo){
        return sysUserRoleService.insert(sysUserRoleConvert.voToDo(vo));
    }

    @PostMapping("updateSysUserRole")
    @ApiOperation("修改用户角色关系")
    public Serializable updateSysUserRole(@Valid @RequestBody ReqSysUserRoleUpdateVo vo){
        return sysUserRoleService.update(sysUserRoleConvert.voToDo(vo));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除用户角色关系")
    public Boolean delete(@PathVariable("id")String id){
        return sysUserRoleService.delete(id) == 1L;
    }

    @PostMapping("/pageList")
    @ApiOperation("分页查询用户角色关系")
    public Pager<RespSysUserRoleVo> pageList(@Valid @RequestBody ReqSysUserRolePageQueryVo vo){
        Pager<SysUserRoleDo> pager = new Pager<>();
        pager.setPageNumber(vo.getPageNumber());
        pager.setPageSize(vo.getPageSize());
        pager = sysUserRoleService.getPager(pager, vo.getSmCriteria());
        pager.setResult(sysUserRoleConvert.doToVo(pager.getResult()));
        Pager<RespSysUserRoleVo> retPager = new Pager<>();
        BeanUtils.copyProperties(pager, retPager);
        return retPager;
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取用户角色关系信息")
    public RespSysUserRoleVo get(@PathVariable("id")String id) {
        return sysUserRoleConvert.doToVo(sysUserRoleService.get(id));
    }
}
